//
//  NewsViewController.m
//  LOL
//
//  Created by lanou3g on 15/11/19.
//  Copyright © 2015年 syx. All rights reserved.
//

#import "NewsViewController.h"
#import "FastLookViewCell.h"
#import "OfficalManager.h"
#import "NewsDetailViewController.h"
#import <MJRefresh.h>

@interface NewsViewController ()

@property (nonatomic,strong) OfficalManager * officalManager;
@property (nonatomic,strong) NSMutableArray * sourceArray;

@end

@implementation NewsViewController

- (NSMutableArray *)sourceArray{
    if (_sourceArray == nil) {
        _sourceArray = [NSMutableArray new];
    }
    return _sourceArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.officalManager = [OfficalManager officalManager];
    
    [self.officalManager getOfficalWithURL:self.urlStr CompletionHandle:^(NSArray *sourceArr) {
        [self.sourceArray addObjectsFromArray:sourceArr];
        [self.tableView reloadData];
    }];
    
    [self.tableView registerClass:[FastLookViewCell class] forCellReuseIdentifier:@"cell"];
    
    // 下拉刷新
    [self normalHeader];
    
    // 上拉加载
    [self normalFooter];
    
}

// 下拉刷新
- (void)normalHeader{
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        sleep(1);
        self.officalManager.UPDrow = NO;
        [self.officalManager getOfficalWithURL:self.urlStr CompletionHandle:^(NSArray *sourceArr) {
            [self.sourceArray addObjectsFromArray:sourceArr];
            [self.tableView reloadData];
        }];
        [self.tableView.mj_header endRefreshing];
    }];
    self.tableView.mj_header = header;

}

// 上拉加载
- (void)normalFooter{
MJRefreshAutoFooter * footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
    sleep(1);
    self.officalManager.UPDrow = YES;
    [self.officalManager getOfficalWithURL:self.urlStr CompletionHandle:^(NSArray *sourceArr) {
        
        [self.sourceArray addObjectsFromArray:sourceArr];
        [self.tableView reloadData];
    }];
    [self.tableView.mj_footer endRefreshing];
}];
    self.tableView.mj_footer = footer;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.sourceArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FastLookViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    FastLookModel * model = self.sourceArray[indexPath.row];
    
    [cell setupViewsWithModel:model];
    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 135;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewsDetailViewController * newsDetailVC = [[NewsDetailViewController alloc]init];
    FastLookModel * model = self.sourceArray[indexPath.row];
    newsDetailVC.idStr = model.article_url;
    [self.navigationController pushViewController:newsDetailVC animated:YES];

}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
